/*
* Unless required by applicable law or agreed to in writing, software 
* distributed under the License is distributed on an "AS IS" BASIS, 
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
* See the License for the specific language governing permissions and 
* limitations under the License. 
*/
#ifndef CMDARG_H_
#define CMDARG_H_
#include "Abstract.h"
#include "stringParser.h"

template <class T = const char*, class P = AbstractParser<T> >
class cmdArg : public AbstractArg
{
public:
	cmdArg(const char* parceStr, const char* desc = NULL, bool m = true) : AbstractArg(parceStr, desc, m) {}
	cmdArg(const char* desc = NULL, bool m = true) : AbstractArg(desc, m) {}
	virtual ~cmdArg() {}
 
	// pure virtual functions implementation
	virtual void Set(std::string& s)
	{
		if (parser != NULL) {
			sscanf(s.c_str(), parser, &value);
//			DBGOUT << "Accepted: " << &value;	
		}
		else {
			AbstractParser<T>* obParser = new P();
			if (!obParser->Parse(s.c_str(), 0, &value))
				return;
		}
		initialized = true;
	}

	
	virtual void Set(char* val, int offset = 0)
	{
		if (parser != NULL) {
			sscanf(val + offset, parser, &value);
//			DBGOUT << "Accepted: " << &value;	
		}
		else {
			AbstractParser<T>* obParser = new P();
			if (!obParser->Parse(val, offset, &value))
				return;
		}
		initialized = true;
	}

	T operator =(cmdArg<T>& ob) { return ob.value; }

	T Value() 
	{
//		char ptrval[22];
//		sprintf(ptrval, "%p", &value);
//		DBGOUT << "\t" << ptrval << std::endl;
		return value; 
	}

	
protected:
	T value;
};

#endif /*CMDARG_H_*/
